package com.rams.jhms.fs;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FSFolder {
	private File folder;
	private List<FSFolder> childs = null;
	
	public FSFolder(File file) {
		if (file.isDirectory() && file.exists())
			this.folder = file;
	}
	
	public String getAbsolutePath() {
		return folder.getAbsolutePath();
	}
	
	private void readChilds() {
		if (childs != null || folder == null)
			return;
		
		childs = new ArrayList<FSFolder>();
		
		File[] childList = folder.listFiles();
		
		if (childList == null)
			return;
		
		for (File child : childList) {
			if (child.isDirectory()) {
				childs.add(new FSFolder(child));
			}
		}
	}
	
	public FSFolder getChild(int index) {
		readChilds();
		return childs.get(index);
	}
	
	public int getChildCount() {
		readChilds();
		if (childs == null)
			return 0;
		return childs.size();
	}
	
	public String toString() {
		if (folder == null)
			return null;
		return folder.getName();
	}
	
	public boolean equals(FSFolder file) {
		return file.getAbsolutePath().equals(folder.getAbsoluteFile());
	}
	
	public List<FSFile> getChildFiles() {
		List<FSFile> res = new ArrayList<FSFile>();
		
		if (folder == null)
			return res; 
		
		File[] files = folder.listFiles(new MediaFileFilter());
		
		for (File file : files) {
			res.add(new FSFile(file));
		}
		return res;
	}
}
